package download.dx.com.httpdownload;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.guoxiaoxing.phoenix.core.PhoenixOption;
import com.guoxiaoxing.phoenix.core.model.MediaEntity;
import com.guoxiaoxing.phoenix.core.model.MimeType;
import com.guoxiaoxing.phoenix.picker.Phoenix;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jwaoo on 2018/1/25.
 */

public class TestActivity extends Activity{
    private ListView listView;
    String [] urls = new String[]{
            "http://192.168.10.241/download/51S888piCamj.mp4",
            "http://192.168.10.241/download/51S888piCamj.mp4"
    };

    private List<DownloadInfo> list = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String dir = getExternalCacheDir().getAbsolutePath();
        for (int i = 0; i < urls.length; i ++ ) {
            DownloadInfo info = new DownloadInfo(urls[i],dir,i+"");
            list.add(info);
//            DownloadManager.addTask(info);
        }
        initView();
        initData();
    }

    public void onpick(View view){
        Phoenix.with()
                .fileType(MimeType.ofImage())//显示的文件类型图片、视频、图片和视频
                .maxPickNumber(10)// 最大选择数量
                .minPickNumber(0)// 最小选择数量
                .spanCount(4)// 每行显示个数
                .enablePreview(true)// 是否开启预览
                .enableCamera(true)// 是否开启拍照
                .enableAnimation(true)// 选择界面图片点击效果
                .enableCompress(false)// 是否开启压缩
                .thumbnailHeight(160)// 选择界面图片高度
                .thumbnailWidth(160)// 选择界面图片宽度
                //如果是在Activity里使用就传Activity，如果是在Fragment里使用就传Fragment
                .start(TestActivity.this, PhoenixOption.TYPE_PICK_MEDIA, 1001);
    }

    private void initData(){

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == RESULT_OK) {
            //返回的数据
            List<MediaEntity> result = Phoenix.result(data);
            Log.e("===","==="+result.size());
//            mMediaAdapter.setData(result);
        }
    }

    private void initView(){
        listView = findViewById(R.id.listview);
        MyAdapter myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
    }

    private class MyAdapter extends BaseAdapter{

        public MyAdapter() {
        }

        @Override
        public int getCount() {
            return urls.length;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item,parent,false);
            }
            MyHolerView holerView = (MyHolerView)convertView.getTag();
            if (holerView == null) {
                holerView = new MyHolerView(convertView);
                convertView.setTag(holerView);
            }
            holerView.loadHolerView(list.get(position),position);
            return convertView;
        }

        public class MyHolerView {
            private TextView textView;
            private ProgressBar progressBar;
            private Button button1;
            private Button button2;
            public MyHolerView(View view) {
                initView(view);
            }

            private void initView(View view){
                textView = view.findViewById(R.id.textView);
                progressBar = view.findViewById(R.id.progressBar);
                button1 = view.findViewById(R.id.button1);
                button2 = view.findViewById(R.id.button2);
            }

            public void loadHolerView(final DownloadInfo info, int position){
                textView.setText(info.getFileName());

                button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (info.getState() != DownloadManager.STATE_PAUSE){
                            DownloadManager.startDownload(info);
                            DownloadManager.setDownloadListener(info, new OnDownloadListener() {
                                @Override
                                public void onDownloadFailed() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(TestActivity.this,"文件下载失败！",Toast.LENGTH_LONG).show();
                                        }
                                    });

                                }

                                @Override
                                public void onFileExists() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(TestActivity.this,"文件已存在！",Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }

                                @Override
                                public void onStop() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            button1.setText("暂停");
                                        }
                                    });

                                }

                                @Override
                                public void onDownProgress(final long progress,final long total) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            progressBar.setProgress((int)(progress*100/total));
                                        }
                                    });

                                }

                                @Override
                                public void onSuccess() {

                                }
                            });
                        }else {
                            DownloadManager.startDownload(info);
                        }
                    }
                });
                button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DownloadManager.downloadStop(info);
                    }
                });


            }
        }
    }

//    public void startDownload(View view){
//        try{
//            DownloadManager.excecute();
//        }catch (Exception ex){
//            ex.printStackTrace();
//        }
//    }
//
//    public void stopDownload(View view){
//        try{
//            DownloadManager.downloadStop();
//        }catch (Exception ex){
//            ex.printStackTrace();
//        }
//    }

//    private String progressStr ="...";
//    private OnDownloadListener onDownloadListener = new OnDownloadListener() {
//        @Override
//        public void onDownloadFailed() {
//            printInfo("download failed" +progressStr);
//            downloadInfo.setState(STATE_PAUSE);
//        }
//
//        @Override
//        public void onDownSuccess() {
//            printInfo("download Success" +progressStr);
//            downloadInfo.setState(STATE_FINISH);
//        }
//
//        @Override
//        public void onDownProgress(long progress, long total) {
//            progressStr = (progress*100/total) +"%";
//            printInfo(progressStr);
//            downloadInfo.setState(STATE_DOWNING);
//        }
//
//        @Override
//        public void onStop() {
//            printInfo("download stop" +progressStr);
//            downloadInfo.setState(STATE_WATTING);
//        }
//    };
//
//    private void printInfo(final String str){
//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                textView.setText(str);
//            }
//        });
//    }
}
